package cn.wildfire.chat.kit.user;

import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.util.HashMap;

import butterknife.BindView;
import cn.wildfire.chat.app.extendsion.classes.ResponseEntity;
import cn.wildfire.chat.app.extendsion.https.OkHttpRequest;
import cn.wildfire.chat.app.extendsion.https.ReqCallBack;
import cn.wildfire.chat.app.extendsion.https.ServerUrlClient;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfire.chat.kit.WfcUIKit;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.model.UserInfoExtra;


/**
 * @Author LiZhong
 * @Date 2019/9/7
 * TODO 修改信推号
 */
public class ChangeAccountActivity extends WfcBaseActivity {

    @BindView(R2.id.nameEditText)
    EditText nameEditText;
    private UserViewModel userViewModel;
    private UserInfo userInfo;

    @Override
    protected void afterViews() {
        super.afterViews();
        userViewModel = WfcUIKit.getAppScopeViewModel(UserViewModel.class);
        userInfo = userViewModel.getUserInfo(userViewModel.getUserId(), false);
        if (userInfo == null) {
            Toast.makeText(this, getString(R.string.user_unexist), Toast.LENGTH_SHORT).show();
            finish();
        }
        initView2();
    }

    @Override
    protected int contentLayout() {
        return R.layout.user_change_account_activity;
    }

    private void initView2() {
        try {
            if (userInfo != null) {
                Gson gson = new Gson();
                UserInfoExtra userInfoExtra = gson.fromJson(userInfo.extra, UserInfoExtra.class);
                nameEditText.setText(userInfoExtra.getCaiba_number());
            }
            nameEditText.setSelection(nameEditText.getText().toString().trim().length());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //nameEditText.addTextChangedListener(new MaxLengthWatcher(16, nameEditText));
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 3);
        setReplaceTitle(null, R.string.modify_account);
        setSearchLayoutVisibility(false);
        getRightTxtView().setBackgroundResource(R.drawable.commom_green_radiusmuch);
        getRightTxtView().setTextColor(getResources().getColor(R.color.white));
        setTextThenBinClickForRightTextView(View.VISIBLE, getString(R.string.complete), new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeAccount();
            }
        });
    }

    private void changeAccount() {
        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .content(getString(R.string.modifying))
                .progress(true, 100)
                .build();
        dialog.show();
        String cailiaoNumber = nameEditText.getText().toString().trim();
        HashMap<String, String> params = new HashMap<>();
        params.put("cailiaoNumber", cailiaoNumber);
        params.put("uid", userInfo.uid);

        OkHttpRequest.getNewInstance(this).requestAsyn(ServerUrlClient.REQUEST_MODIFY_ACCOUNT, OkHttpRequest.TYPE_POST_JSON, params, new ReqCallBack<Object>() {
            @Override
            public void onReqSuccess(Object result) {
                try {
                    String ret = result.toString();
                    if (ret != null) {
                        ResponseEntity<JsonObject, String> responseEntity = new ResponseEntity(result.toString());
                        if (responseEntity.getErrorCode().equals("0")) {
                            Toast.makeText(ChangeAccountActivity.this, getString(R.string.modify_success), Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                            finish();
                        } else {
                            Toast.makeText(ChangeAccountActivity.this, responseEntity.message, Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                            finish();
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onReqFailed(String errorMsg) {
                Toast.makeText(ChangeAccountActivity.this, getString(R.string.modify_failure), Toast.LENGTH_SHORT).show();
                dialog.dismiss();
                finish();
            }
        }).request();
    }

}
